﻿using LY.DataAccess.Repository;
using LY.Entity;
using LY.Entity.Entities;
using LY.Entity.PageSearch;
using LY.Entity.SearchEntity;
using LY.Interface;
//using LY.Interface.Service;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace LY.Business.ServiceImpl
{
    public class ProjectService : IProjectService
    {
        ProjectRepository _repo;
        public ProjectService()
        {
            _repo = new ProjectRepository();
        }

        public PagedList<R_ProjectClass> QueryProjectClass(PageView view)
        {
            return _repo.QueryProjectClass(view);
        }

        public PagedList<Entity.R_ProjectClass> QueryAllProjectClass(PageView view)
        {
            return _repo.QueryAllProjectClass(view);
        }

        public int SaveProjectClass(Entity.R_ProjectClass model)
        {
            if (model.Id <= 0) //新增
            {
                return (int)_repo.Insert(model);
            }
            else
            {
                return (int)_repo.Update(model);
            }
        }

        public int DeleteProjectClass(int id)
        {
            return _repo.DeleteProjectClass(id);
        }

        public R_ProjectClass GetModel(int Id)
        {
            return _repo.GetModel(Id);
        }
        public List<R_ProjectClass> GetProjectClassListModel()
        {
            return _repo.GetProjectClassListModel();
        }


        //查询项目
        public PagedList<ProjectEx> QueryProject(ProjectSearch search, PageView view)
        {
            return _repo.QueryProject(search, view);
        }
        public PagedList<ProjectEx> QueryMyProject(ProjectSearch search, PageView view)
        {
            return _repo.QueryProject_EXList(search, view);
        }

        //我发起的项目
        public PagedList<ProjectEx> QueryProjectMyProject(ProjectSearch search, PageView view)
        {
            return _repo.QueryProjectMyProject_EXList(search, view);
        }

        //查询出当前会员关注的项目
        public PagedList<ProjectEx> QueryProjectAttention(ProjectSearch search, PageView view)
        {
            return _repo.QueryProjectAttention_EXList(search, view);
        }

        //查询购买过的众筹商品
        public PagedList<ProjectEx> QueryBoughtProject(ProjectSearch search, PageView view)
        {
            return _repo.QueryBoughtProject(search, view);
        }

        //获得一个实体
        public R_Project GetProject(int? id)
        {
            return _repo.GetProject(id);
        }


        public int SaveProject(R_Project model)
        {
            if (model.Id <= 0) //新增
            {
                //这里添加的时候给众筹标一个唯一的标示号
                string biaoNo = _repo.GetProjectBiaoNo();
                if (string.IsNullOrEmpty(biaoNo))//是空就是当天的第一个
                {
                    biaoNo = "QJSP" + DateTime.Now.ToString("yyyyMMdd") + "0001";
                }
                else
                {
                    string lyDate = "QJSP" + DateTime.Now.ToString("yyyyMMdd");
                    int no = int.Parse(biaoNo.Substring(biaoNo.Length - 4, 4));//取后四位
                    if (no > 0 && no < 10)
                    {
                        no++;
                        if (no != 10)
                        {
                            biaoNo = lyDate + "000" + no;
                        }
                        else
                        {
                            biaoNo = lyDate + "00" + no;
                        }
                    }
                    else if (no >= 10 && no <= 99)
                    {
                        no++;
                        if (no != 100)
                        {
                            biaoNo = lyDate + "00" + no;
                        }
                        else
                        {
                            biaoNo = lyDate + "0" + no;
                        }
                    }
                    else if (no >= 100 && no <= 999)
                    {
                        no++;
                        if (no != 1000)
                        {
                            biaoNo = lyDate + "0" + no;
                        }
                        else
                        {
                            biaoNo = lyDate + no;
                        }
                    }
                    else
                    {
                        biaoNo = lyDate + no;
                    }
                }
                model.BiaoNo = biaoNo;
                return (int)_repo.Insert(model);
            }
            else
            {
                //编辑
                return (int)_repo.Update(model);
            }
        }



        //获取项目投资档位
        public PagedList<R_ProjectItem_EX> QueryProjectItem(PageView view, int projectId)
        {
            return _repo.QueryProjectItem(view, projectId);
        }

        //查询项目档位的某一项
        public R_ProjectItem GetProjectItem(int id)
        {
            return _repo.GetProjectItem(id);
        }

        //保存项目档位
        public int SaveProjectItem(R_ProjectItem projectItem)
        {
            if (projectItem.Id <= 0) //新增
            {
                return (int)_repo.Insert(projectItem);
            }
            else
            {
                return (int)_repo.Update(projectItem);
            }
        }

        //查询订单
        public PagedList<R_ProjectOrder_EX> QueryProjectOrder(PageView view, ProjectOrderSearch search)
        {
            return _repo.QueryProjectOrder(view, search);
        }

        //获取一条投资记录
        public R_ProjectOrder GetProjectOrder(int id)
        {
            return _repo.GetProjectOrder(id);
        }


        //保存订单
        public int SaveProjectOrder(R_ProjectOrder projectOrder)
        {
            if (projectOrder.Id > 0)//编辑
            {
                return _repo.Update(projectOrder);
            }
            else
            {
                return (int)_repo.Insert(projectOrder);
            }
        }

        //查询出拓展的项目列表
        public List<R_Project_EX> QueryProject_EXList(ProjectSearch search)
        {
            return _repo.QueryProject_EXList(search);
        }

        public PagedList<ProjectEx> QueryProject_EXList(ProjectSearch search, PageView view)
        {

            return _repo.QueryProject_EXList(search, view);
        }


        public R_Project_EX GetProject_EX(int id)
        {
            return _repo.GetProject_EX(id);
        }

        //查询每个档位投资信息
        public List<R_ProjectOrder_EX> QueryInvestmentByItem(int projectId)
        {
            return _repo.QueryInvestmentByItem(projectId);
        }

        //查询每个项目一共筹集多少资金
        public decimal GetAllAmount(int projectId)
        {
            return _repo.GetAllAmount(projectId);
        }

        //查询某个项目的支持人数
        public int GetOrderCount(int projectId)
        {
            return _repo.GetOrderCount(projectId);
        }

        //查询某个人一共发起过几个项目
        public int GetProjectCount(int memberId)
        {
            return _repo.GetProjectCount(memberId);
        }

        //查询最近投资本项目的人员列表
        public List<U_Member_EX> QueryInvestmentMember(int projectId)
        {
            return _repo.QueryInvestmentMember(projectId);
        }
       

        //根据当前会员ID查询与之对应的项目
        public PagedList<R_Project_EX> GetProjectList(int id, PageView view)
        {
            return _repo.GetProjectList(id, view);
        }
        #region 项目扩展
        public R_ProjectExpand QueryProjectExpand(int projectid)
        {
            return _repo.QueryProjectExpand(projectid);
        }
        public R_ProjectExpand SaveProjectExpand(int projectid, int type)
        {
            return _repo.SaveProjectExpand(projectid, type);
        }
        //取消关注，关注减1 
        public R_ProjectExpand CancelProjectExpand(int projectid)
        {
            return _repo.CancelProjectExpand(projectid);
        }

        #endregion



        #region 项目时间轴
        public int SaveProjectDatelinetree(R_ProjectDatelinetree model)
        {
            return _repo.SaveProjectDatelinetree(model);
        }

        public IList<R_ProjectDatelinetree> QueryProjectDatelinetree(int projectid)
        {
            return _repo.QueryProjectDatelinetree(projectid);
        }

        public R_ProjectDatelinetree GetProjectDatelinetree(int id)
        {
            return _repo.GetProjectDatelinetree(id);
        }


        #endregion


        public bool IsProjectOwnedUser(int pid, int uid)
        {
            return _repo.IsProjectOwnedUser(pid, uid);
        }


        public List<R_Project_EX> CainiXihuang()
        {
            return _repo.CainiXihuang();
        }

        //提交问题
        public int SubmitQuestion(R_projectQuestion project)
        {
            return (int)_repo.Insert(project);
        }


        public List<R_ProjectQuestion_EX> GetMyQuestionList(string mid, string pid)
        {
            return _repo.GetMyQuestionList(mid, pid);
        }


        public PagedList<R_ProjectQuestion_EX> QuestionManagement(PageView view, string id)
        {
            return _repo.QuestionManagement(view, id);
        }


        public List<R_ProjectQuestion_EX> GetQuestionList(string id, string mid)
        {
            return _repo.GetQuestionList(id, mid);
        }


        public int GetQIdByMemberId(string mid, int? type)
        {
            return _repo.GetQIdByMemberId(mid, type);
        }


        public int ReplyQuestion(R_ProjectQuestion_EX pq)
        {
            return (int)_repo.Insert(pq);
        }


        public int DeleteQuestion(string mid, string pid)
        {
            return _repo.DeleteQuestion(mid, pid);
        }

        //删除已经保存的项目
        public int DeleteSaveProject(int id)
        {
            return _repo.DeleteSaveProject(id);
        }

        /*
        public int AdoptQuestion(string id)
        {
            return _repo.AdoptQuestion(id);
        }*/


        public List<R_ProjectQuestion_EX> GetQuestionList(string pid)
        {
            return _repo.GetQuestionList(pid);
        }
        //根据订单编号获取订单详情
        public R_ProjectOrder GerProjectOrderBySerialNumber(string serialNumber)
        {
            return _repo.GerProjectOrderBySerialNumber(serialNumber);
        }
        //修改订单状态
        public int UpdateOrderPayStatus(int status, string orderid)
        {
            return _repo.UpdateOrderPayStatus(status, orderid);
        }
        //根据订单编号删除订单
        public int DeleteOrder(string orderid)
        {
            return _repo.DeleteOrder(orderid);
        }

        public ProjectItem GetProjectItem()
        {
            return _repo.GetProjectItem();
        }


        public PagedList<ProjectEx> QueryHotProject(ProjectSearch search, PageView view)
        {
            return _repo.QueryHotProject(search, view);
        }


        public List<ProjectEx> QueryHotProject()
        {
            return _repo.QueryHotProject();
        }


        public PagedList<U_Member_EX> QueryInvestmentMemberList(PageView view, int projectId)
        {
            return _repo.QueryInvestmentMemberList(view, projectId);
        }


        public int ConfirmOrder(string id)
        {
            return _repo.ConfirmOrder(id);
        }

        public int InsertPorjectFlow(R_ProjectFlow flow)
        {
            return _repo.InsertPorjectFlow(flow);
        }

        public  IList<R_ProjectFlow> SelectPorjectFlow(int porjectid)
        {
            return _repo.SelectPorjectFlow(porjectid);
        }

        /// <summary>
        /// 首页数据统计 项目总数，注册人数，累计投资
        /// </summary>
        /// <returns></returns>
        public Change.Index GetIndexStatics()
        {
            return _repo.GetIndexStatics();
        }


        public List<R_ProjectOrder> QueryProjectOrderList(int projectId, int payStatus)
        {
            return _repo.QueryProjectOrderList(projectId, payStatus);
        }
    }
}
